package org.zfes.snowier.cloud.zbss.cms.model.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.model.service.ICmsModelItemService;
import org.zfes.snowier.cloud.zbss.cms.model.dao.CmsModelItemMapper;
import org.zfes.snowier.cloud.zbss.cms.model.model.CmsModelItem;

import java.util.stream.Stream;
@Service
public class CmsModelItemServiceImpl implements ICmsModelItemService{
 	@Autowired
	private CmsModelItemMapper cmsModelItemMapper;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsModelItem(CmsModelItem cmsModelItem) {
		ZBeanUtil.setCreateInfo(cmsModelItem);
		ZBeanUtil.validateBean(cmsModelItem);
		cmsModelItemMapper.insertSelective(cmsModelItem);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsModelItem> loadCmsModelItemById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsModelItemMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsModelItem(CmsModelItem cmsModelItem) {
		ZBeanUtil.setUpdatedInfo(cmsModelItem);
		ZBeanUtil.validateBean(cmsModelItem);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsModelItem(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		setCmsModelItemStatus( id, Byte.valueOf("-1"));
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsModelItemStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsModelItemMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsModelItemDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsModelItemMapper.selectListCount(pm), cmsModelItemMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsModelItem> loadCmsItemsByModelId(Long modelId) {
		if(ZObjectUtil.isNotNull(modelId)) {
			return Lists.newArrayList();
		}
		return cmsModelItemMapper.selectByModelId( modelId);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void createCmsModelItemBatch(List<CmsModelItem> cmsModelItemList,Long modelId) {
		ZAssert.notEmpty(cmsModelItemList, "modelItem参数错误！");
		if(!ZObjectUtil.isNotNull(modelId)) {
			ZAlert.serviceLogicalError("modelId不能为空");
		}
		CmsModelItem modelItem = new CmsModelItem();
		
		for(int i=0;i<cmsModelItemList.size();i++) {
			//取出modelItem
			modelItem=cmsModelItemList.get(i);
			//更新时间、创建时间、modelId修改
			ZBeanUtil.setCreateInfo(modelItem);
			modelItem.setModelId(modelId);
			cmsModelItemMapper.insertSelective(modelItem);
		}
		
	}
	
}